package com.ybg.ebs_memory.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class Util {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in),1024);
    private static Random rand = new Random();

    public static String dateFormat(long time){
        return sdf.format(new Date(time));
    }

    public static String input(String title, String regex){
        System.out.print(title);
        do{
            String line;
            try {
                line = in.readLine();
            } catch (IOException e) {
                System.out.print("输入异常，"+title);
                continue;
            }
            if(line.matches(regex)){
                return line;
            }
            System.out.print("非法输入，"+title);
        }while(true);
    }

    public static int choose(String title,int minChoice,int maxChoice){
        String line = input(title,"\\d+");
        int choice;
        do{
            choice = Integer.parseInt(line);
            if(choice==0 || choice>=minChoice && choice<=maxChoice){
                break;
            }
            System.out.print("输入越界，"+title);
        }while(!line.matches("\\d+"));
        return choice;
    }

    public static float randPrice(int from, int to){
        return from + rand.nextInt(to-from);
    }
}
